package railRoad.io.files;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import railRoad.Schedule;
import railRoad.Route;
import railRoad.Station;

/**
 * XML files parser
 * 
 * @see FileWriteRead
 * @author Rukin
 * 
 */
public class XML extends FileWriteRead {

	@Override
	public void read() {
		XMLInputFactory inputFactory = XMLInputFactory.newInstance();
		try {
			XMLStreamReader reader = inputFactory.createXMLStreamReader(this
					.getIn());
			process(reader);
		}// try
		catch (XMLStreamException e) {
			System.out.println(e.getMessage());
		}// catch
	}// read

	private void process(XMLStreamReader reader) throws XMLStreamException {

		Schedule schedule = Schedule.getInstance();
		String nameXML;

		Route route = null;
		Station station = null;

		int id;
		int availableTicket;
		String name;
		float altitude, longitude;

		while (reader.hasNext()) {
			int type = reader.next();

			if (type == XMLStreamConstants.START_ELEMENT) {
				nameXML = reader.getLocalName();

				if (nameXML.equals("Route")) {
					id = Integer.parseInt(reader.getAttributeValue(null, "id"));
					availableTicket = Integer.parseInt(reader
							.getAttributeValue(null, "availableTicket"));
					route = new Route(id, availableTicket);

					schedule.addRoute(route);
				}// if Route

				if (nameXML.equals("Station")) {
					id = Integer.parseInt(reader.getAttributeValue(null, "id"));
					name = reader.getAttributeValue(null, "name");
					altitude = Float.parseFloat(reader.getAttributeValue(null,
							"altitude"));
					longitude = Float.parseFloat(reader.getAttributeValue(null,
							"longitude"));

					station = new Station(id, name, altitude, longitude);
					route.addStation(station);
				}// if Station

			}// if START_ELEMENT

		}// while
	}// parse

	@Override
	public void write() throws XMLStreamException {
		Schedule scheldure = Schedule.getInstance();
		XMLOutputFactory factory = XMLOutputFactory.newInstance();
		XMLStreamWriter xtw = factory.createXMLStreamWriter(this.getOut(),
				"UTF-8");

		xtw.writeStartDocument("UTF-8", "1.0");
		xtw.writeStartElement("road:Railroad");
		xtw.writeAttribute("xmlns:road", "http://www.example.com/RailRoad");
		xtw.writeAttribute("xmlns:xsi",
				"http://www.w3.org/2001/XMLSchema-instance");
		xtw.writeAttribute("xsi:schemaLocation",
				"http://www.example.com/RailRoad RailRoad.xsd");

		Route route = null;
		Station station = null;

		for (int i = 0; i < scheldure.size(); i++) {
			route = scheldure.getRouteAt(i);
			xtw.writeStartElement("Route");
			xtw.writeAttribute("id", String.valueOf(route.getRoute_id()));
			xtw.writeAttribute("availableTicket",
					String.valueOf(route.getAvailableTicket()));

			for (int j = 0; j < route.size(); j++) {
				station = route.getStationAt(j);
				xtw.writeStartElement("Station");
				xtw.writeAttribute("id",
						String.valueOf(station.getStation_id()));
				xtw.writeAttribute("name", station.getStationName());
				xtw.writeAttribute("altitude",
						String.valueOf(station.getAltitude()));
				xtw.writeAttribute("longitude",
						String.valueOf(station.getLongitude()));

				xtw.writeEndElement(); // Station
			}
			xtw.writeEndElement(); // Route
		}
		xtw.writeEndElement(); // Railroad

		xtw.writeEndDocument();

		xtw.flush();
		xtw.close();
	}// write

}
